package com.example.guideapp.activity;


import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.guideapp.R;
import com.example.guideapp.adapter.MyChooseFestivalMsgAdapter;
import com.example.guideapp.adapter.MyChooseWishMsgAdapter;
import com.example.guideapp.bean.FestivalMessage;
import com.example.guideapp.bean.WishTypeMessage;
import com.example.guideapp.fragment.Fragment1;
import com.example.guideapp.fragment.Fragment2;
import com.example.guideapp.utils.DatabaseManage;
import com.example.guideapp.utils.MySqliteDatabaseHelper;

import java.util.List;

public class ChooseMsgActivity extends AppCompatActivity {

    private ListView mListView;
    private TextView tv_title;

    private List<FestivalMessage> fList;
    private List<WishTypeMessage> wList;

    private MyChooseFestivalMsgAdapter fAdapter;
    private MyChooseWishMsgAdapter wAdapter;

    private String type;

    private MySqliteDatabaseHelper helper;

    private LayoutInflater inflater;
    private AlertDialog.Builder alertDialog;
    private View dialogView;
    private EditText et_send_content;
    private Button btn_send_share;
    private Button btn_send_copy;
    private Button btn_send_message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_msg);

        initView();

        type = getIntent().getStringExtra(Fragment1.TYPE);

        if (type.equals(Fragment1.FESTIVAL)){
            typeFestival();

        }else if (type.equals(Fragment2.WISHTYPE)){
            typeWishType();
        }

    }

    private void typeWishType() {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from WisType_Message where wishType_id = "+getIntent().getIntExtra(Fragment2.WISHTYPEID,0),null);
        wList = DatabaseManage.wishTypeMessagesCursorToList(cursor);
        db.close();

        wAdapter = new MyChooseWishMsgAdapter(this,wList);
        mListView.setAdapter(wAdapter);

        tv_title.setText(getIntent().getStringExtra(Fragment2.WISHTYPENAME));

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                showAlertDialog(wList.get(i).getContent());

            }
        });
    }

    private void typeFestival() {
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from Festival_Message where festival_id = "+getIntent().getIntExtra(Fragment1.FESTIVALID,0),null);
        fList = DatabaseManage.festivalMessageCursorToList(cursor);
        db.close();

        fAdapter = new MyChooseFestivalMsgAdapter(this,fList);
        mListView.setAdapter(fAdapter);

        tv_title.setText(getIntent().getStringExtra(Fragment1.FESTIVALNAME));

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                showAlertDialog(fList.get(i).getContent());

            }
        });
    }

    private void initView() {
        helper = DatabaseManage.getInstance(this);
        inflater = getLayoutInflater();

        mListView = (ListView) findViewById(R.id.lv_chooseMsg);
        tv_title = (TextView) findViewById(R.id.tv_choose_title);
    }


    private void showAlertDialog(final String  content){

        initAlertDialogView(content);

        initAlertDialogClick(content);

        if (alertDialog == null){
            Log.e("aaa", "new");
            alertDialog = new AlertDialog.Builder(ChooseMsgActivity.this).setView(dialogView);
        }

        alertDialog.setView(dialogView);

        alertDialog.show();
        
        Log.e("aaa", dialogView.toString());
    }

    private void initAlertDialogClick(final String content) {
        btn_send_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i =new Intent();
                i.setAction(Intent.ACTION_SEND);
                i.putExtra(Intent.EXTRA_TEXT,content);
                i.setType("text/plain");
                startActivity(Intent.createChooser(i,"分享到"));
            }
        });

        btn_send_copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                //cm.setText(content);
                ClipData clip = ClipData.newPlainText("message",content);
                cm.setPrimaryClip(clip);
                Toast.makeText(ChooseMsgActivity.this, "复制成功", Toast.LENGTH_SHORT).show();
            }
        });

        btn_send_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (content.length()>70){
                    Toast.makeText(ChooseMsgActivity.this, "短信内容大于70长度，将分2条短信发送", Toast.LENGTH_SHORT).show();
                }
                Uri toUri = Uri.parse("smsto:");//联系人地址
                Intent intent = new Intent(android.content.Intent.ACTION_SENDTO,toUri);
                intent.putExtra("sms_body",content);
                startActivity(intent);
            }
        });
    }

    private void initAlertDialogView(String content) {
        dialogView = inflater.inflate(R.layout.send_message, (ViewGroup) findViewById(R.id.ll_send));

        et_send_content = (EditText) dialogView.findViewById(R.id.et_send_msg);
        btn_send_share = (Button) dialogView.findViewById(R.id.btn_send_share);
        btn_send_copy = (Button) dialogView.findViewById(R.id.btn_send_copy);
        btn_send_message = (Button) dialogView.findViewById(R.id.btn_send_message);

        et_send_content.setText(content);
        et_send_content.setSelection(content.length());
    }


}
